package com.zf.shuashua.advert;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;

import com.vise.xsnow.http.callback.ACallback;
import com.zf.shuashua.AdvertUtils;
import com.zf.shuashua.ShuaApplication;
import com.zf.shuashua.bean.AdvertReport;
import com.zf.shuashua.bean.BaseData;
import com.zf.shuashua.manager.ActionThirdReportManager;
import com.zf.shuashua.utils.Util;

import java.lang.ref.WeakReference;

import cn.admobiletop.adsuyi.ad.ADSuyiRewardVodAd;
import cn.admobiletop.adsuyi.ad.data.ADSuyiRewardVodAdInfo;
import cn.admobiletop.adsuyi.ad.error.ADSuyiError;
import cn.admobiletop.adsuyi.ad.listener.ADSuyiRewardVodAdListener;
import cn.admobiletop.adsuyi.util.ADSuyiAdUtil;
import cn.admobiletop.adsuyi.util.ADSuyiToastUtil;

/**
 * 激励视频展示
 * AdMobile激励视频
 */
public class RewardAdvertMobileManager {
    private static final String TAG = "RewardAdvertMobile";

    private ADSuyiRewardVodAd rewardVodAd;
    private String mAdvertType = AdvertConstants.REWARD_TYPE_TASK;
    private String mCodeId = "0429ec89a0da621ebf";
    private String mReportID = "";

    private static RewardAdvertMobileManager instance;
    private WeakReference<Activity> weakReference;
    private boolean isComplete = false;

    private RewardAdvertMobileManager() {
    }

    public static RewardAdvertMobileManager getInstance() {
        if (null == instance) {
            synchronized (RewardAdvertMobileManager.class) {
                if (null == instance) {
                    instance = new RewardAdvertMobileManager();
                }
            }
        }
        return instance;
    }

    public void showAdvert(Activity activity, String advertType, String reportId, String codeId, OnRewardListener listener) {
        this.weakReference = new WeakReference<>(activity);
        this.mAdvertType = advertType;
        this.mReportID = reportId;
        if(!TextUtils.isEmpty(codeId)){
            this.mCodeId = codeId;
        }

        isComplete = false;

        init(activity, listener);
        loadAd();
    }

    private String planId;
    private String uuiThirdAdvert;
    private void init(Activity activity, OnRewardListener listener){
        // 创建激励视频广告实例
        rewardVodAd = new ADSuyiRewardVodAd(activity);
        uuiThirdAdvert = Util.getUUID32();
        planId = System.identityHashCode(rewardVodAd) + "";

        // 设置仅支持的广告平台，设置了这个值，获取广告时只会去获取该平台的广告，null或空字符串为不限制，默认为null，方便调试使用，上线时建议不设置
//        rewardVodAd.setOnlySupportPlatform(ADSuyiDemoConstant.REWARD_VOD_AD_ONLY_SUPPORT_PLATFORM);
        // 设置激励视频广告监听
        rewardVodAd.setListener(new ADSuyiRewardVodAdListener() {
            @Override
            public void onVideoCache(ADSuyiRewardVodAdInfo adSuyiRewardVodAdInfo) {
                // 目前汇量和Inmobi走了该回调之后才准备好
                Log.d(TAG, "onVideoCache...");
            }

            @Override
            public void onVideoComplete(ADSuyiRewardVodAdInfo adSuyiRewardVodAdInfo) {
                Log.d(TAG, "onVideoComplete...");
                isComplete = true;
                if (listener != null) {
                    listener.onVideoComplete();
                }
            }

            @Override
            public void onVideoError(ADSuyiRewardVodAdInfo adSuyiRewardVodAdInfo, ADSuyiError adSuyiError) {
                Log.d(TAG, "onVideoError..." + adSuyiError.toString());
                reportRequestRend(adSuyiError.getCode() + "", adSuyiError.getError());
            }

            @Override
            public void onReward(ADSuyiRewardVodAdInfo adSuyiRewardVodAdInfo) {
                Log.d(TAG, "onReward...");
                isComplete = true;
            }

            @Override
            public void onAdReceive(ADSuyiRewardVodAdInfo rewardVodAdInfo) {
//                RewardVodAdActivity.this.rewardVodAdInfo = rewardVodAdInfo;
//                ADSuyiToastUtil.show(ShuaApplication.getContext(), "激励视频广告获取成功");
                Log.d(TAG, "onAdReceive...");
                weakReference.get().runOnUiThread(() -> showAdvert(activity, rewardVodAdInfo));
                reportRequestSucc(1);
            }

            @Override
            public void onAdExpose(ADSuyiRewardVodAdInfo adSuyiRewardVodAdInfo) {
                Log.d(TAG, "onAdExpose...");
                AdReportManager.getInstance().reportAdvertAction(uuiThirdAdvert, planId, AdvertReport.EventType.VideoStart, getReportType(),"8");
            }

            @Override
            public void onAdClick(ADSuyiRewardVodAdInfo adSuyiRewardVodAdInfo) {
                Log.d(TAG, "onAdClick...");
                if (listener != null) {
                    listener.onAdClick();
                }
                AdReportManager.getInstance().reportAdvertAction(uuiThirdAdvert, planId, AdvertReport.EventType.VideoClick, getReportType(), "8");
            }

            @Override

            public void onAdClose(ADSuyiRewardVodAdInfo adSuyiRewardVodAdInfo) {
                Log.d(TAG, "onAdClose...");
                dealClose(planId, listener);
            }

            @Override
            public void onAdFailed(ADSuyiError adSuyiError) {
//                ADSuyiToastUtil.show(ShuaApplication.getContext(), "广告获取失败");
                if (adSuyiError != null) {
                    reportRequestFail(adSuyiError.getCode() + "", adSuyiError.getError());
                }
                weakReference.get().runOnUiThread(() -> {
                    if (listener != null) {
                        listener.onVideoError();
                    }
                });
            }
        });
    }

    private void loadAd() {
        // 加载激励视频广告，参数为广告位ID
        rewardVodAd.loadAd(mCodeId);
        reportRequestStart();
    }

    private void showAdvert(Activity activity, ADSuyiRewardVodAdInfo rewardVodAdInfo) {
        ADSuyiAdUtil.showRewardVodAdConvenient(activity, rewardVodAdInfo);
    }

    private void reportRequestStart(){
        ActionThirdReportManager.getInstance().reportRequestStart(8, 2, mCodeId, 1);
    }

    private void reportRequestSucc(int return_num) {
        ActionThirdReportManager.getInstance().reportRequestSucc(8, 2, mCodeId, return_num);
    }

    private void reportRequestFail(String error_code, String error_msg){
        ActionThirdReportManager.getInstance().reportRequestFail(8, 2, mCodeId, 1, error_code, error_msg);
    }

    private void reportRequestRend(String error_code, String error_msg){
        ActionThirdReportManager.getInstance().reportRequestRend(8, 2, mCodeId, error_code, error_msg);
    }

    private void dealClose(String planId, OnRewardListener listener){
        if(!isComplete){
            return;
        }
        AdReportManager.getInstance().reportAdvertAction(uuiThirdAdvert, planId, AdvertReport.EventType.VideoEnd, getReportType(), "8", new ACallback<BaseData>() {
            @Override
            public void onSuccess(BaseData data) {
                if (data != null && data.isStatus()) {
                    weakReference.get().runOnUiThread(() -> {
                        if (listener != null) {
                            listener.onAdClose();
                        }
                    });
                }
            }

            @Override
            public void onFail(int errCode, String errMsg) {

            }
        });
    }

    private String getReportType() {
        return AdvertUtils.getReportType(mAdvertType, mReportID);
    }
}
